<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    fetch('http://localhost:3000/fdata')
    .then(function (data) {
        // text属于fetchAPI的一部分，它返回promise对象
        return data.text()
    })
    .then(function (data) {
        // 最终数据
        console.log(data)
    })

    fetch('http://localhost:3000/books?id=123',{
        method:'get'
    })
        .then(function (data) {
            return data.text()
        })
        .then(function (data) {
            console.log(data)
        })

    fetch('http://localhost:3000/books/456',{
        method:'get'
    })
        .then(function (data) {
            return data.text()
        })
        .then(function (data) {
            console.log(data)
        })

    fetch('http://localhost:3000/books/789',{
        method:'delete'
    })
        .then(function (data) {
            return data.text()
        })
        .then(function (data) {
            console.log(data)
        })

    fetch('http://localhost:3000/books',{
        method:'post',
        body:'uname=lisi&pwd=123',
        headers:{
            'Content-Type':'application/x-www-form-urlencoded'
        }
    })
        .then(function (data) {
            return data.text()
        })
        .then(function (data) {
            console.log(data)
        })
    // JSON
    fetch('http://localhost:3000/books',{
        method:'post',
        body:JSON.stringify({
            uname:'张三',
            pwd:'456'
        }),
        headers:{
            'Content-Type':'application/json'
        }
    })
        .then(function (data) {
            return data.text()
        })
        .then(function (data) {
            console.log(data)
        })

    fetch('http://localhost:3000/books/123',{
        method:'put',
        body:JSON.stringify({
            uname:'张三',
            pwd:'789'
        }),
        headers:{
            'Content-Type':'application/json'
        }
    })
        .then(function (data) {
            return data.text()
        })
        .then(function (data) {
            console.log(data)
        })

    fetch('http://localhost:3000/json')
        .then(function (data) {
            return data.json()
        })
        .then(function (data) {
            console.log(data)
        })

    fetch('http://localhost:3000/json')
    .then(function (data) {
        return data.text()
    })
    .then(function (data) {
        var obj=JSON.parse(data)
        console.log(obj)
    })
</script>
</body>
</html>
